package com.om.app;

import java.util.Collections;
import java.util.List;

import com.om.data.DataManager;
import com.om.data.QuestionDS;
import com.om.data.Quiz;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class QuestionActivity extends Activity {
	/** Called when the activity is first created. */
	DataManager dm;
	Quiz quiz;
	int qNum = 0;
	int correctAnswer;
	QuestionDS questinDS;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.question);
		dm = DataManager.getInstance();
		quiz = dm.prepareQuiz();

		TextView optionText = (TextView) findViewById(R.id.answerOption1Text);
		optionText.setText("A");

		optionText = (TextView) findViewById(R.id.answerOption2Text);
		optionText.setText("B");

		optionText = (TextView) findViewById(R.id.answerOption3Text);
		optionText.setText("C");

		optionText = (TextView) findViewById(R.id.answerOption4Text);
		optionText.setText("D");
		
		optionText = (TextView) findViewById(R.id.categoryText);
		switch (dm.getQuestionType()) {
			case OLYMPIC_GAMES:
				optionText.setText("Games Evolution");
				break;
			case ATHLETES:
				optionText.setText("Athletes");
				break;
			case COUNTRIES:
				optionText.setText("Countries");
				break;
			case EXTRA:
				optionText.setText("Bonus");
				break;
			case LONDON:
				optionText.setText("London 2012");
				break;
			}
		
		gameInit();
		onNext(null);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode==Activity.RESULT_OK){
			finish();
	    }
	}
	public void onBack(View v) {
		finish();
	}
	
	private void gameInit(){
		ImageView img = (ImageView) findViewById(R.id.x1);
        img.setImageResource(R.drawable.x2);
		img = (ImageView) findViewById(R.id.x2);
        img.setImageResource(R.drawable.x2);
		img = (ImageView) findViewById(R.id.x3);
        img.setImageResource(R.drawable.x2);
	}
	
	public void onNext(View v) {
		
		if (qNum >= quiz.getNumOfQuestion() || quiz.getErrorsNum() > 2) {
			Intent i = new Intent();
			i.setClass(this, EndOfGameActivity.class);
			startActivityForResult(i, 125);
			finish();
			return;
		}
		
		initVisibility();
		qNum++;

		TextView qNumText = (TextView) findViewById(R.id.numbersText);
		qNumText.setText(qNum + "/" + quiz.getNumOfQuestion());

		questinDS = quiz.getQuestions().get(qNum-1);

		TextView qQText = (TextView) findViewById(R.id.questionText);
		qQText.setText(questinDS.getQuestion());

		correctAnswer = (int) (Math.random() * 4);

		List<String> answers = questinDS.getAnswers();
		Collections.swap(answers, 0, correctAnswer);

		TextView text = (TextView) findViewById(R.id.answer1Text);
		text.setText(new String(questinDS.getAnswers().get(0)));
		text.setTextColor(Color.BLACK);

		text = (TextView) findViewById(R.id.answer2Text);
		text.setText(new String(questinDS.getAnswers().get(1)));
		text.setTextColor(Color.BLACK);

		text = (TextView) findViewById(R.id.answer3Text);
		text.setText(new String(questinDS.getAnswers().get(2)));
		text.setTextColor(Color.BLACK);

		text = (TextView) findViewById(R.id.answer4Text);
		text.setText(new String(questinDS.getAnswers().get(3)));
		text.setTextColor(Color.BLACK);		 

		Collections.swap(answers, correctAnswer, 0);
		
		ImageView butn = (ImageView) findViewById(R.id.nextArrow);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answerOption1);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answerOption2);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answerOption3);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answerOption4);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answer_background1);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answer_background2);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answer_background3);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answer_background4);
		butn.setEnabled(true);
	}

	private void initVisibility(){
		ImageView img = (ImageView) findViewById(R.id.comment_background);
		img.setVisibility(View.INVISIBLE);
		TextView text = (TextView) findViewById(R.id.commentText);
		text.setVisibility(View.INVISIBLE);

		img = (ImageView) findViewById(R.id.answerOption1Correct);
		img.setVisibility(View.INVISIBLE);
		img = (ImageView) findViewById(R.id.answerOption2Correct);
		img.setVisibility(View.INVISIBLE);
		img = (ImageView) findViewById(R.id.answerOption3Correct);
		img.setVisibility(View.INVISIBLE);
		img = (ImageView) findViewById(R.id.answerOption4Correct);
		img.setVisibility(View.INVISIBLE);
		
		img = (ImageView) findViewById(R.id.answerOption1Wrong);
		img.setVisibility(View.INVISIBLE);
		img = (ImageView) findViewById(R.id.answerOption2Wrong);
		img.setVisibility(View.INVISIBLE);
		img = (ImageView) findViewById(R.id.answerOption3Wrong);
		img.setVisibility(View.INVISIBLE);
		img = (ImageView) findViewById(R.id.answerOption4Wrong);
		img.setVisibility(View.INVISIBLE);
		
		text = (TextView) findViewById(R.id.answerOption1Text);
		text.setVisibility(View.VISIBLE);
		text = (TextView) findViewById(R.id.answerOption2Text);
		text.setVisibility(View.VISIBLE);
		text = (TextView) findViewById(R.id.answerOption3Text);
		text.setVisibility(View.VISIBLE);
		text = (TextView) findViewById(R.id.answerOption4Text);
		text.setVisibility(View.VISIBLE);	
	}
	
	public synchronized void act(View v) {
		
		ImageView butn = (ImageView) findViewById(R.id.nextArrow);
		butn.setEnabled(true);
		butn = (ImageView) findViewById(R.id.answerOption1);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answerOption2);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answerOption3);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answerOption4);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answer_background1);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answer_background2);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answer_background3);
		butn.setEnabled(false);
		butn = (ImageView) findViewById(R.id.answer_background4);
		butn.setEnabled(false);
		
		
		String txtName = getResources().getResourceName(v.getId());
		txtName = txtName.substring(txtName.length() - 1, txtName.length());
		int chosenAnswer = Integer.valueOf(txtName);
		correctAnswer++;//so it will start at 1 and not on 0

		displayAnswers(chosenAnswer);
		displayComment();
		
	}

	private synchronized void displayAnswers(int chosenAnswer){
		ImageView img = null;
		displayCorrectAnswer(chosenAnswer);
	
		if (chosenAnswer != correctAnswer) {
			quiz.addError();
			switch (quiz.getErrorsNum()) {
			case 1:
				img = (ImageView) findViewById(R.id.x1);
		        img.setImageResource(R.drawable.x);
				break;
			case 2:
				img = (ImageView) findViewById(R.id.x2);
		        img.setImageResource(R.drawable.x);
				break;
			case 3:
				img = (ImageView) findViewById(R.id.x3);
		        img.setImageResource(R.drawable.x);
				break;
			}
			displayWrongAnswer(chosenAnswer);			
		}
		displayCorrectAnswer(correctAnswer);
		

	}

	private synchronized void displayCorrectAnswer(int correctAnswer){
		TextView textInv = null;
		TextView text = null;
		ImageView img = null;
		switch (correctAnswer) {
		case 1:
			text = (TextView) findViewById(R.id.answer1Text);
			img = (ImageView) findViewById(R.id.answerOption1Correct);
			textInv = (TextView) findViewById(R.id.answerOption1Text);
			break;
		case 2:
			text = (TextView) findViewById(R.id.answer2Text);
			img = (ImageView) findViewById(R.id.answerOption2Correct);
			textInv = (TextView) findViewById(R.id.answerOption2Text);
			break;
		case 3:
			text = (TextView) findViewById(R.id.answer3Text);
			img = (ImageView) findViewById(R.id.answerOption3Correct);
			textInv = (TextView) findViewById(R.id.answerOption3Text);
			break;
		case 4:
			text = (TextView) findViewById(R.id.answer4Text);
			img = (ImageView) findViewById(R.id.answerOption4Correct);
			textInv = (TextView) findViewById(R.id.answerOption4Text);
			break;
		}
		img.setVisibility(View.VISIBLE);
		int color = getResources().getColor(R.color.green_dark);
		text.setTextColor(color);
		textInv.setVisibility(View.INVISIBLE);
	}
	
	private synchronized void displayWrongAnswer(int chosenAnswer){
		TextView textInv = null;
		TextView text = null;
		ImageView img = null;
		switch (chosenAnswer) {
		case 1:
			text = (TextView) findViewById(R.id.answer1Text);
			img = (ImageView) findViewById(R.id.answerOption1Wrong);
			textInv = (TextView) findViewById(R.id.answerOption1Text);
			break;
		case 2:
			text = (TextView) findViewById(R.id.answer2Text);
			img = (ImageView) findViewById(R.id.answerOption2Wrong);
			textInv = (TextView) findViewById(R.id.answerOption2Text);
			break;
		case 3:
			text = (TextView) findViewById(R.id.answer3Text);
			img = (ImageView) findViewById(R.id.answerOption3Wrong);
			textInv = (TextView) findViewById(R.id.answerOption3Text);
			break;
		case 4:
			text = (TextView) findViewById(R.id.answer4Text);
			img = (ImageView) findViewById(R.id.answerOption4Wrong);
			textInv = (TextView) findViewById(R.id.answerOption4Text);
			break;
		}
		img.setVisibility(View.VISIBLE);
		text.setTextColor(Color.RED);
		textInv.setVisibility(View.INVISIBLE);
	}	
	
	
	private synchronized void displayComment() {
		
		TextView text = null;
		ImageView img = null;
		if (questinDS.getComment() != null && !questinDS.getComment().equals("") ) {
			text = (TextView) findViewById(R.id.commentText);
			String com = questinDS.getComment();
			text.setText(com);
			text.setTextColor(Color.WHITE);
			text.setVisibility(View.VISIBLE);
			img = (ImageView) findViewById(R.id.comment_background);
			img.setVisibility(View.VISIBLE);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.om_menu, menu);
	    return true;
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
        case R.id.about:
    		Intent i = new Intent();
    		i.setClass(this, AboutUsActivity.class);		
    		startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }	    	
    }

}
